package com.elitesland.demo.controller.backend.com;

import com.elitesland.b2c.com.service.ComAreaService;
import com.elitesland.b2c.com.vo.param.ComAreaPageParamVO;
import com.elitesland.b2c.com.vo.resp.ComAreaBaseRespVO;
import com.elitesland.b2c.com.vo.resp.ComAreaDetailRespVO;
import com.elitesland.b2c.com.vo.resp.ComAreaTreeRespVO;
import com.elitesland.b2c.com.vo.save.ComAreaSaveVO;
import com.elitesland.core.base.ApiResult;
import com.elitesland.core.base.PagingVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "区域管理")
@ApiSupport(author = "wang shao", order = 2)
@RestController
@RequestMapping(value = "/com/area", produces = MediaType.APPLICATION_JSON_VALUE)
public class ComAreaController {

    @Autowired
    private ComAreaService comAreaService;

    @ApiOperation("新增区域")
    @ApiOperationSupport(order = 1)
    @PostMapping("")
    public ApiResult<Long> save(@RequestBody ComAreaSaveVO saveVO) {
        return comAreaService.save(saveVO);
    }

    @ApiOperation("修改区域")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true)
    })
    @PutMapping("/{id}")
    public ApiResult<Long> update(@PathVariable Long id, @RequestBody ComAreaSaveVO saveVO) {
        return comAreaService.update(id, saveVO);
    }

    @ApiOperation("获取区域详情")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true)
    })
    @GetMapping("/{id}")
    public ApiResult<ComAreaDetailRespVO> get(@PathVariable Long id) {
        return comAreaService.get(id);
    }

    @ApiOperation("删除区域数据")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true)
    })
    @DeleteMapping("/{id}")
    public ApiResult<Long> delete(@PathVariable Long id) {
        return comAreaService.delete(id);
    }

    @ApiOperation("批量删除区域数据")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "区域ID", required = true, allowMultiple = true)
    })
    @DeleteMapping("/delete")
    public ApiResult<List<Long>> deleteBatch(@RequestBody List<Long> ids) {
        return comAreaService.delete(ids);
    }

    @ApiOperation("分页查询区域数据")
    @ApiOperationSupport(order = 5)
    @PostMapping("/search")
    public ApiResult<PagingVO<ComAreaDetailRespVO>> search(@RequestBody ComAreaPageParamVO param) {
        return comAreaService.search(param);
    }

    @ApiOperation("切换启用状态")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true)
    })
    @PatchMapping("/{id}/enabled")
    public ApiResult<Long> updateEnable(@PathVariable Long id) {
        return comAreaService.updateEnable(id);
    }

    @ApiOperation("批量启用或禁用")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "区域ID", required = true, allowMultiple = true),
            @ApiImplicitParam(name = "enabled", value = "是否启用", required = true)
    })
    @PatchMapping("/enabled/{enabled}")
    public ApiResult<List<Long>> updateEnableBatch(@RequestBody List<Long> ids, @PathVariable Boolean enabled) {
        return comAreaService.updateEnable(ids, enabled);
    }

    @ApiOperation("查询下级区域")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "区域ID", required = true)
    })
    @GetMapping("/{id}/children")
    public ApiResult<List<ComAreaBaseRespVO>> findByParentId(@PathVariable Long id, String name) {
        return comAreaService.queryByParent(id, name);
    }

    @ApiOperation("获取地区树")
    @ApiOperationSupport(order = 11)
    @GetMapping("/tree")
    public ApiResult<List<ComAreaTreeRespVO>> tree() {
        return comAreaService.queryForTree();
    }

}